package de.fzi.kasma.learner.genetic;



import java.text.DecimalFormat;

import de.fzi.kasma.learner.util.Util;
import ec.EvolutionState;
import ec.util.Code;
import ec.util.Parameter;
import ec.vector.VectorIndividual;

public class SLIndividual extends VectorIndividual{

	
	public double[] coeffs;
	
	
	/**
	 * 
	 */
	
	private static final long serialVersionUID = -3278272741575670128L;
	

	@Override
	public Object clone() {
	    SLIndividual clone =  (SLIndividual) (super.clone()); 
	    double[] dummy = new double[coeffs.length];
        clone.coeffs= dummy;
        for(int x=0;x<coeffs.length;x++) 
            clone.coeffs[x] = coeffs[x]; 
        clone.fitness = fitness;
        return clone;
    }
	
	@Override
	public void reset(EvolutionState state, int thread) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean equals(Object ind) {

		if (!getClass().equals(ind.getClass())) 
	            return false;

	    SLIndividual other = (SLIndividual)ind;
	        if (coeffs.length != other.coeffs.length) return false;
	        for(int x=0;x<coeffs.length;x++)
	            if (coeffs[x]!=(other.coeffs[x])) return false; 
	        return true;
	}
	
    public void defaultMutate(EvolutionState state, int thread) {
    	
		SLIndividual clone = (SLIndividual) this.clone();
		double[] values = clone.coeffs;
		for (int j = 0; j < values.length; j++) {
			if (state.random[thread].nextBoolean()) {

				if (values[j] > SLSpecies.min)
					values[j] = SLSpecies.min;
				else
					values[j] = Util.nextDoubleInRange(SLSpecies.min, SLSpecies.max);
			}
		} this.coeffs = values;
    }
    
    public void defaultCrossover(EvolutionState state, int thread, SLIndividual other) {
    	
    		
    		double[] coeffs1 = this.coeffs;
    		double[] coeffs2 = other.coeffs;
    		boolean[] swap = new boolean[coeffs1.length];
    		for (int i = 0; i < swap.length; i++) {
    			swap[i] = state.random[thread].nextBoolean();
    		}
    		for (int i = 0; i < swap.length; i++) {
    			if (swap[i]) {
    				double tmp = coeffs1[i];
    				coeffs1[i] = coeffs2[i];
    				coeffs2[i] = tmp;
    			}
    		}
    		this.coeffs = coeffs1;
    		other.coeffs = coeffs2;
    	}
    	


	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
	        return 0;
	}

	@Override
	public Parameter defaultBase() {

		return RLDefaults.base().push(P_INDIVIDUAL);
	}
	@Override
	public void setup(EvolutionState state, Parameter base) {
		// TODO Auto-generated method stub
		super.setup(state, base);
		
		this.evaluated=false;
		
	}
	
    public void printIndividualForHumans(final EvolutionState state,
            final int log, 
            final int verbosity)

            {
    	
//    		super.printIndividualForHumans(state, log, verbosity);
    		
                state.output.println("The support vectors: ", verbosity, log);

                for(int i =0; i<coeffs.length; i++){

                	if(coeffs[i]>0){
                		
                	DecimalFormat df = new DecimalFormat("#.####");

                	state.output.print(i+"  :  "+df.format(coeffs[i])+" ; ", verbosity, log);
                	}
                }
//
//            fitness.printFitnessForHumans(state,log,verbosity);
//            state.output.println( genotypeToStringForHumans(), verbosity, log );
            }

}
